#ifndef IMPROFILECONTROL_H_
#define IMPROFILECONTROL_H_
#include <QMap>
#include <QtXml>
#include <QtGui/QWidget>

class QDomDocument;
class QtVariantProperty;
class ImVariantManager;
class QtVariantPropertyManager;
class QtVariantEditorFactory;
class QtTreePropertyBrowser;
class QtProperty;

#define TEMPLATE_FILE "share.tmpl.xml"

typedef struct PROFILEGROUP
{
	QString name;
	QString text;
	QString iconName;
} ProfileGroup;

typedef struct PROFILEITEM
{
	QString id;
	QString name;
	QString text;
	QString content;
	QString iconName;
	QString url;
	ProfileGroup *group;
} ProfileItem;

typedef struct PROPERTYITEM
{
	QString key;
	QVariant value;
	QtVariantProperty *item;	
	
} PropertyItem;

typedef QMap<QString, QDomDocument*> DocumentMap;
typedef QMap<QString, QVariant> PropertyMap;

class ImProfileControl: public QWidget
{
	Q_OBJECT

public:
	ImProfileControl(QWidget *parent = 0);
	~ImProfileControl();
	void loadTemplate(const QString &profileDir);
	void templateGroups();
	void templateProfiles();
	bool profileItemElement(const QString &itemTagName, const QDomDocument &doc, QDomElement &retElement);
	QString itemValue(const QDomElement &e, const QString &itemTagName = QString());
	void templateParse(const QString &url);
	void displayData();
	bool findItemData(const QString &_key, PropertyItem *&rvt);
	bool findItemData(const QtVariantProperty *_item, PropertyItem *&rvt);
	bool valueUpdate( const QString &key, const QVariant & value );

private:
	QString textValue(const QDomElement &e);
	QString getNodeValue(const QDomElement& element);
	
	bool save(const QDomDocument &dom, QIODevice *device);
	void mergeElements(const QDomElement& src1,const QDomElement& src2, QList<QDomElement>& list);
	QDomDocument *getElementItem(const QString url=QString(TEMPLATE_FILE)) const;
	void parseChildElement(const QDomElement& element, QtVariantProperty *parentItem = 0);
	void addProperty(QtVariantProperty *property, const QString &id);	
	
	void updateExpandState();
	ProfileGroup *findGroupByName(const QString &groupName);
	void testSaveDocment();

public slots:
	void valueChanged(QtProperty *property, const QVariant &value);

public:	
	QList<QDomElement> m_elementList;
	QList<ProfileGroup *> m_groupList;
	QList<ProfileItem  *> m_profileList;
	
private:
	QtVariantPropertyManager *m_variantManager;
	QtVariantEditorFactory *m_variantFactory;
	QtTreePropertyBrowser *m_propertyEditor;

	QString m_templatePath;
	QDomDocument *m_tmplDocument;
	DocumentMap m_templateMap;
	
	//QtVariantProperty *m_item;
	QMap<QtProperty *, QString> propertyToId;
	QMap<QString, QtVariantProperty *> idToProperty;
	QMap<QtProperty *, QDomElement> propertyToDomElement;
	QMap<QString, bool> idToExpanded;
	QList<PropertyItem *> m_itemData;
};

#endif
